<?php
namespace Home\Controller;
use Think\Controller;
use WechatSdk\Wechat;
use Mp\Controller\ApiController;
//use Mp\Controller\ApiController;

class IndexController extends Controller {
	private $options = array(
    	'token'				=>	'', 				
    	'encodingaeskey'	=>	'', 		
    	'appid'				=>	'', 				
    	'appsecret'			=>	'' 			
    );

    public function _initialize() {
        if (!is_file(SITE_PATH.'/Data/install.lock')) {             // 如果框架未安装，则跳转到安装页面
            $this->redirect('Install/Index/index');
        }
        global $_G;
        $_G['site_path'] = SITE_PATH . '/';
        $_G['site_url'] = str_replace('index.php', '', 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
        $_G['addons_path'] = str_replace('./', $_G['site_path'], ADDON_PATH);
        $_G['addons_url'] = $_G['site_url'] . str_replace('./', '', ADDON_PATH);
        $_G['module_name'] = strtolower(MODULE_NAME);
        $_G['controller_name'] = strtolower(CONTROLLER_NAME);
        $_G['action_name'] = strtolower(ACTION_NAME);
        add_hook('rbac', 'Common\Behavior\RbacBehavior');
        hook('rbac');                                           // 执行权限检测钩子
        $this->user_id = session(C('USER_AUTH_KEY'));           
        $this->user_info = get_user_info();
        $this->user_access = D('User/User')->get_user_access($this->user_id);
    }
     


    public function index() {
		$mpid = get_mpid(I('mpid'));
		init_fans();                                                                //  通过网页授权拉取粉丝信息
        $openid = get_openid();                                                     // 缓存用户openid
		
        $model=M('goods');
        $map['_string'] = "status='上架' OR status='下架'";
        $goods=$model->where($map)->select();
        $index=0;
        foreach ($goods as $k => $v) {
            $downtime[$v['id']]=$goods[$index]['downtime'];
            $index++;
            $nowtime=time();
            $difftime[$v['id']]['diff']=$nowtime-$downtime[$v['id']];
            if($difftime[$v['id']]['diff']>=0){
                $data[$v['id']]=$difftime[$v['id']]['diff'];
                $model->where(array('id'=>$v['id']))->setField('status','下架');
            }else{
                $data[$v['id']]=$difftime[$v['id']]['diff'];
                $model->where(array('id'=>$v['id']))->setField('status','上架');
            }
        }
		
		//测试输出 -zsyk
		print_r($openid);
		print_r($_SESSION);
		
        $this->assign('goods',$goods);
        $this->display();
    }
	

}